﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using BLL.Services.Books;
using Web.Infrastructure.Models;

namespace Web.Areas.Admin.Models.Books
{
    public class BookCreateModel : BaseModel, IValidatableObject
    {
        public BookCreateModel(Book book)
        {
            ToModel(book, this);
            Authors = new int[0];
            Categories = new int[0];
        }

        public BookCreateModel()
        {
            Authors = new int[0];
            Categories = new int[0];
        }

        [Required(ErrorMessage = @"File name is required!")]
        [Display(Name = "File name")]
        [UIHint("Upload")]
        public string FileName { get; set; }

        [Required(ErrorMessage = @"Avatar name is required!")]
        [Display(Name = "Avatar")]
        [UIHint("Upload")]
        public string Avatar { get; set; }

        [Required(ErrorMessage = @"Date of publish is required!")]
        [Display(Name = "Date of publish")]
        public DateTime DateOfPublish { get; set; }

        [Required(ErrorMessage = @"Short content is required!")]
        [Display(Name = "Short content")]
        [UIHint("MultilineText")]
        [StringLength(1000)]
        public string ShortContent { get; set; }

        [Required(ErrorMessage = @"Name is required!")]
        [Display(Name = "Name")]
        [Remote("Validate", "Books", "Admin", AdditionalFields = "Authors", ErrorMessage = "Book with the same name and authors already exists.")]
        public string Name { get; set; }

        [Display(Name = "User")]
        [UIHint("Select")]
        [Required(ErrorMessage = @"User is required!")]
        [Range(1, int.MaxValue, ErrorMessage = "Please select user.")]
        public int UserId { get; set; }

        [Required(ErrorMessage = @"Author is required!")]
        [Display(Name = "Authors")]
        [UIHint("MultiSelect")]
        [Remote("Validate", "Books", "Admin", AdditionalFields = "Name", ErrorMessage = "Book with the same name and authors already exists.")]
        public int[] Authors { get; set; }

        [Required(ErrorMessage = @"Category is required!")]
        [Display(Name = "Categories")]
        [UIHint("MultiSelect")]
        public int[] Categories { get; set; }

        /// <summary>
        /// Gets the book service.
        /// </summary>
        /// <value>
        /// The book service.
        /// </value>
        protected static IBookService BookService
        {
            get
            {
                return DependencyResolver.Current.GetService<IBookService>();
            }
        }

        public static explicit operator Book(BookCreateModel model)
        {
            var book = new Book();
            ToEntity(model, book);
            book.AuthorIds = model.Authors;
            book.CategoryIds = model.Categories;
            return book;
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var result = new List<ValidationResult>();
            if (Authors.Where(x => x != 0).Count() == 0)

                result.Add(new ValidationResult("Please add one or move authors", new[] { "Authors" }));

            if (Categories.Where(x => x != 0).Count() == 0)
                result.Add(new ValidationResult("Please add one or move categories", new[] { "Categories" }));

            if (!BookService.ValidateBook(0, Name, Authors))
            {
                result.Add(
                    new ValidationResult(
                        "Book with the same name and authors already exists.", new[] { "Name", "Authors" }));
            }
            return result;
        }


    }
}